package com.woohua.thread.cyclicbarrier;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;

/***
 * @title MakeCarBody
 * @description
 * @author woo hua
 * @version 1.0.0
 * @create 2023/3/17 14:52
 **/
public class MakeCarBody implements Runnable {

    private CyclicBarrier cyclicBarrier;

    public MakeCarBody(CyclicBarrier cyclicBarrier) {
        this.cyclicBarrier = cyclicBarrier;
    }

    @Override
    public void run() {
        //造完后继续造下一个
        while (true) {
            try {
                System.out.println("Starting make car body...");
                long millis = ((int) (1 + Math.random() * (5 - 1 + 1))) * 1000;

                TimeUnit.MILLISECONDS.sleep(millis);

                System.out.println("Making car body is finished.");

                // 等待其他部件制造完成
                cyclicBarrier.await();
            } catch (InterruptedException | BrokenBarrierException e) {
                e.printStackTrace();
            }
        }
    }
}
